// pages/myself/info/bindMobile.js
var util = require('../../../utils/utils.js');
var network = require('../../../utils/network_util.js');
var api = require('../../../utils/api.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    mobile: "",
    smsCode: "",
    second: 60,
    showTime: "获取验证码",
    hasCode: false,
    isSubmit: false,
    size: 0,
    size1: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "绑定手机号",
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },

  handlePhone: function (e) {
    this.setData({
      size: e.detail.value.length
    });
    var reg = "^((13[0-9])|(14[5|7])|(15([0-9]))|(17([0-9]))|(18[0-9]))\\d{8}$";
    var patrn = new RegExp(reg);
    if (patrn.exec(e.detail.value)) {
      if (this.data.size1 == 6) {
        this.setData({
          isSubmit: true
        });
      }
      this.setData({
        hasCode: true
      });
    } else {
      this.setData({
        hasCode: false,
        isSubmit: false
      });
    }
  },

  handleCode: function (e) {
    this.setData({
      size1: e.detail.value.length
    });
    if (e.detail.value.length == 6) {
      if (this.data.size == 11) {
        this.setData({
          isSubmit: true
        });
      }
    } else {
      this.setData({
        isSubmit: false
      });
    }
  },

  /**
   * 获取验证码
   */
  formPhone: function (e) {
    this.setData({
      hasCode: false
    });
    var that = this;
    var phone = e.detail.value.phone;
    util.countdown(that);
    network._post_json(this, api.authCode, { mobile: phone, type: 1 }, success);
  },

  /**
   * 绑定手机号
   */
  formSubmit: function (e) {
    var phone = e.detail.value.phone;
    var code = e.detail.value.code;
    this.setData({
      mobile: phone
    })
    
    if (code == this.data.smsCode) {
      network._post_json(this, api.modifyMobile, { mobile: phone, smsCode: code }, bindSuccess);
    }
  },
})

// 获取验证码
var success = function (that, res) {
  if (res.data.data) {
    that.setData({
      smsCode: res.data.data.smsCode
    })
  } else {
    wx.showToast({
      title: res.data.error,
    })
  }
}

// 绑定成功结果
var bindSuccess = function (that, res) {
  /**
     * 获取个人及商家信息
     */
  if (app.globalData.userAndTenantInfo) {
    app.globalData.userAndTenantInfo.mobile = that.data.mobile
  } 
  //获取页面栈
  var pages = getCurrentPages();
  if (pages.length > 1) {
    //上一个页面实例对象
    var prePage = pages[pages.length - 2];
    if (prePage.refresh && typeof (prePage.refresh) == "function") {
      prePage.refresh(that.data.mobile);
    }
  }
  wx.navigateBack();
}
